package com.example.shengyin.frontController;


import com.example.shengyin.entity.Seeker;
import com.example.shengyin.service.SeekerService;
import com.example.shengyin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.UUID;

@RestController
@RequestMapping("/front/seeker")
@Api(tags = "FrontSeekerController", description = "岗位应聘者接口")
public class FrontSeekerController {

    @Value("${upload-path}")
    private String path;

    @Value("${server.servlet.context-path}")
    private String contexPath;

    @Autowired
    private SeekerService service;

    /**
     * 上传简历
     * doresume
     */

    @ApiOperation("应聘者简历上传,这是前端接口")
    @PostMapping("/doresume")
    public Result UploadResume(@RequestParam(value = "file") MultipartFile file) {

        if (file != null && !file.isEmpty()) {
            File f = new File(path);
            if (!f.exists())
                f.mkdirs();
            //原来的文件名
            String filename = file.getOriginalFilename();
            String houzhui = filename.substring(filename.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;
            System.out.println("newName=" + newName);

            try {
                file.transferTo(new File(f, newName));

                return Result.success("img/" + newName, "上传成功");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.error(415, "上传失败");
    }


    /**
     * 存储岗位投递人员的信息，含简历上传
     */

    @ApiOperation("存储岗位投递人员姓名、手机号、简历等信息，表单提交,前端接口")
    @PostMapping("/upload")
    public Result resumeUpload(Seeker seeker, String fileUrl) {
        Timestamp d = new Timestamp(System.currentTimeMillis());
        seeker.setPosttime(d);
        seeker.setResumeUrl(fileUrl);

        int insert = service.insertSeeker(seeker);
        if (insert == 1) {
            return Result.success("简历上传成功");
        }
        return Result.error("简历上传失败");
    }

}
